home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / bin_d2.zoo / lisp / info.el < prev    next >
Lisp/Scheme  |  1991-12-02  |  24KB  |  702 lines

  1. ;; Info package for Emacs  -- could use a "create node" feature.
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. (provide 'info)
  21.  
  22. (defvar Info-history nil
  23.   "List of info nodes user has visited.
  24. Each element of list is a list (FILENAME NODENAME BUFFERPOS).")
  25.  
  26. (defvar Info-enable-edit nil
  27.   "Non-nil means the \\[Info-edit] command in Info can edit the current node.")
  28.  
  29. (defvar Info-enable-active-nodes t
  30.   "Non-nil allows Info to execute Lisp code associated with nodes.
  31. The Lisp code is executed when the node is selected.")
  32.  
  33. (defvar Info-directory nil
  34.   "Default directory for Info documentation files.")
  35.  
  36. (defvar Info-current-file nil
  37.   "Info file that Info is now looking at, or nil.")
  38.  
  39. (defvar Info-current-subfile nil
  40.   "Info subfile that is actually in the *info* buffer now,
  41. or nil if current info file is not split into subfiles.")
  42.  
  43. (defvar Info-current-node nil
  44.   "Name of node that Info is now looking at, or nil.")
  45.  
  46. (defvar Info-tag-table-marker (make-marker)
  47.   "Marker pointing at beginning of current Info file's tag table.
  48. Marker points nowhere if file has no tag table.")
  49.  
  50. (defun info ()
  51.   "Enter Info, the documentation browser."
  52.   (interactive)
  53.   (if (get-buffer "*info*")
  54.       (switch-to-buffer "*info*")
  55.     (Info-directory)))
  56.  
  57. ;; Go to an info node specified as separate filename and nodename.
  58. ;; no-going-back is non-nil if recovering from an error in this function;
  59. ;; it says do not attempt further (recursive) error recovery.
  60. (defun Info-find-node (filename nodename &optional no-going-back)
  61.   ;; Convert filename to lower case if not found as specified.
  62.   ;; Expand it.
  63.   (if filename
  64.       (let (temp)
  65.     (setq filename (substitute-in-file-name filename))
  66.     (setq temp (expand-file-name filename
  67.                      ;; Use Info's default dir
  68.                      ;; unless the filename starts with `./'.
  69.                      (if (not (string-match "^\\./" filename))
  70.                      Info-directory)))
  71.     (if (file-exists-p temp)
  72.         (setq filename temp)
  73.       (setq temp (expand-file-name (downcase filename) Info-directory))
  74.       (if (file-exists-p temp)
  75.           (setq filename temp)
  76.         (error "Info file %s does not exist"
  77.            (expand-file-name filename Info-directory))))))
  78.   ;; Record the node we are leaving.
  79.   (if (and Info-current-file (not no-going-back))
  80.       (setq Info-history
  81.         (cons (list Info-current-file Info-current-node (point))
  82.           Info-history)))
  83.   ;; Go into info buffer.
  84.   (switch-to-buffer "*info*")
  85.   (or (eq major-mode 'Info-mode)
  86.       (Info-mode))
  87.   (widen)
  88.   (setq Info-current-node nil)
  89.   (unwind-protect
  90.       (progn
  91.     ;; Switch files if necessary
  92.     (or (null filename)
  93.         (equal Info-current-file filename)
  94.         (let ((buffer-read-only nil))
  95.           (setq Info-current-file nil
  96.             Info-current-subfile nil)
  97.           (erase-buffer)
  98.           (insert-file-contents filename t)
  99.           (set-buffer-modified-p nil)
  100.           (setq default-directory (file-name-directory filename))
  101.           ;; See whether file has a tag table.  Record the location if yes.
  102.           (set-marker Info-tag-table-marker nil)
  103.           (goto-char (point-max))
  104.           (forward-line -8)
  105.           (or (equal nodename "*")
  106.           (not (search-forward "\^_\nEnd tag table\n" nil t))
  107.           (let (pos)
  108.             ;; We have a tag table.  Find its beginning.
  109.             ;; Is this an indirect file?
  110.             (search-backward "\nTag table:\n")
  111.             (setq pos (point))
  112.             (if (save-excursion
  113.               (forward-line 2)
  114.               (looking-at "(Indirect)\n"))
  115.             ;; It is indirect.  Copy it to another buffer
  116.             ;; and record that the tag table is in that buffer.
  117.             (save-excursion
  118.               (let ((buf (current-buffer)))
  119.                 (set-buffer (get-buffer-create " *info tag table*"))
  120.                 (setq case-fold-search t)
  121.                 (erase-buffer)
  122.                 (insert-buffer-substring buf)
  123.                 (set-marker Info-tag-table-marker
  124.                     (match-end 0))))
  125.              (set-marker Info-tag-table-marker pos))))
  126.           (setq Info-current-file
  127.             (file-name-sans-versions buffer-file-name))))
  128.     (if (equal nodename "*")
  129.         (progn (setq Info-current-node nodename)
  130.            (Info-set-mode-line))
  131.       ;; Search file for a suitable node.
  132.       ;; First get advice from tag table if file has one.
  133.       ;; Also, if this is an indirect info file,
  134.       ;; read the proper subfile into this buffer.
  135.       (let ((guesspos (point-min)))
  136.         (if (marker-position Info-tag-table-marker)
  137.         (save-excursion
  138.           (set-buffer (marker-buffer Info-tag-table-marker))
  139.           (goto-char Info-tag-table-marker)
  140.           (if (search-forward (concat "Node: " nodename "\177") nil t)
  141.               (progn
  142.             (setq guesspos (read (current-buffer)))
  143.             ;; If this is an indirect file,
  144.             ;; determine which file really holds this node
  145.             ;; and read it in.
  146.             (if (not (eq (current-buffer) (get-buffer "*info*")))
  147.                 (setq guesspos
  148.                   (Info-read-subfile guesspos))))
  149.             (error "No such node: \"%s\"" nodename))))
  150.         (goto-char (max (point-min) (- guesspos 1000))))
  151.       ;; Now search from our advised position (or from beg of buffer)
  152.       ;; to find the actual node.
  153.       (let ((regexp (concat "Node: *" (regexp-quote nodename) " *[,\t\n]")))
  154.         (catch 'foo
  155.           (while (search-forward "\n\^_" nil t)
  156.         (forward-line 1)
  157.         (let ((beg (point)))
  158.           (forward-line 1)
  159.           (if (re-search-backward regexp beg t)
  160.               (throw 'foo t))))
  161.           (error "No such node: %s" nodename)))
  162.       (Info-select-node)))
  163.     ;; If we did not finish finding the specified node,
  164.     ;; go back to the previous one.
  165.     (or Info-current-node no-going-back
  166.     (let ((hist (car Info-history)))
  167.       (setq Info-history (cdr Info-history))
  168.       (Info-find-node (nth 0 hist) (nth 1 hist) t)
  169.       (goto-char (nth 2 hist)))))
  170.   (goto-char (point-min)))
  171.  
  172. (defun Info-read-subfile (nodepos)
  173.   (set-buffer (marker-buffer Info-tag-table-marker))
  174.   (goto-char (point-min))
  175.   (search-forward "\n\^_")
  176.   (let (lastfilepos
  177.     lastfilename)
  178.     (forward-line 2)
  179.     (catch 'foo
  180.       (while (not (looking-at "\^_"))
  181.     (if (not (eolp))
  182.         (let ((beg (point))
  183.           thisfilepos thisfilename)
  184.           (search-forward ": ")
  185.           (setq thisfilename  (buffer-substring beg (- (point) 2)))
  186.           (setq thisfilepos (read (current-buffer)))
  187.           (if (> thisfilepos nodepos)
  188.           (throw 'foo t))
  189.           (setq lastfilename thisfilename)
  190.           (setq lastfilepos thisfilepos)))))
  191.     (set-buffer (get-buffer "*info*"))
  192.     (or (equal Info-current-subfile lastfilename)
  193.     (let ((buffer-read-only nil))
  194.       (setq buffer-file-name nil)
  195.       (widen)
  196.       (erase-buffer)
  197.       (insert-file-contents lastfilename)
  198.       (set-buffer-modified-p nil)
  199.       (setq Info-current-subfile lastfilename)))
  200.     (goto-char (point-min))
  201.     (search-forward "\n\^_")
  202.     (+ (- nodepos lastfilepos) (point))))
  203.  
  204. ;; Select the info node that point is in.
  205. (defun Info-select-node ()
  206.   (save-excursion
  207.    ;; Find beginning of node.
  208.    (search-backward "\n\^_")
  209.    (forward-line 2)
  210.    ;; Get nodename spelled as it is in the node.
  211.    (re-search-forward "Node:[ \t]*")
  212.    (setq Info-current-node
  213.      (buffer-substring (point)
  214.                (progn
  215.                 (skip-chars-forward "^,\t\n")
  216.                 (point))))
  217.    (Info-set-mode-line)
  218.    ;; Find the end of it, and narrow.
  219.    (beginning-of-line)
  220.    (let (active-expression)
  221.      (narrow-to-region (point)
  222.                (if (re-search-forward "\n[\^_\f]" nil t)
  223.                (prog1
  224.                 (1- (point))
  225.                 (if (looking-at "[\n\^_\f]*execute: ")
  226.                 (progn
  227.                   (goto-char (match-end 0))
  228.                   (setq active-expression
  229.                     (read (current-buffer))))))
  230.              (point-max)))
  231.      (if Info-enable-active-nodes (eval active-expression)))))
  232.  
  233. (defun Info-set-mode-line ()
  234.   (setq mode